package simri.desktop.gui;


import gnu.io.CommPortIdentifier;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.SwingConstants;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class JPConfig extends javax.swing.JPanel {

	/*{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
		} catch(Exception e) {
			e.printStackTrace();
		}
	}*/

	private JLabel jLabelConfiguracoes;
	private JLabel jLabelEscolherBaudRate;
	private JComboBox jComboBoxEscolherBaudRate;
	private JComboBox jComboBoxEscolherPorta;
	private JLabel jLabelEscolherPorta;
	private JButton jButtonReconhecerPortas;
	
	protected String[] portas;
    protected Enumeration listaDePortas;
    protected String portaEscolhida;
    protected int baudrate;
    protected String enviarString;
    protected char enviarChar;
    protected String recebido;


    public int getBaudRate() {
    	return this.baudrate;
    }
    
    public String getPortaEscolhida() {
    	return this.portaEscolhida;
    }
    public void SerialCom (){
        listaDePortas = CommPortIdentifier.getPortIdentifiers();
    }

/*
* método para retornar as portas disponíveis
*/

    public String[] ObterPortas(){
        return portas;
    }

/*
 * armazenar uma lista das portas seriais do sistema disponíveis para a comunicação
 */

    protected void ListarPortas(){
        int i = 0;
        portas = new String[10];
        while (listaDePortas.hasMoreElements()) {
            CommPortIdentifier ips =
            (CommPortIdentifier)listaDePortas.nextElement();
            portas[i] = ips.getName();
            jComboBoxEscolherPorta.insertItemAt(portas[i],i);
            i++;
        }
    }

/*
 *  identificar se a porta selecionada existe e está tudo em funcionamento com ela
 */

    public boolean PortaExiste(String COMp){
        String temp;
        boolean e = false;
        while (listaDePortas.hasMoreElements()) {
            CommPortIdentifier ips = (CommPortIdentifier)listaDePortas.nextElement();
            temp = ips.getName();
            if (temp.equals(COMp)== true) {
                e = true;
            }
        }
        return e;
    }

	/**
	* Auto-generated main method to display this 
	* JPanel inside a new JFrame.
	*/
		
	public JPConfig(JButton jbCancelar, JButton jbSalvar) {
		super();
		initGUI(jbCancelar,jbSalvar);
	}
	
	private void initGUI(JButton jbCancelar, JButton jbSalvar) {
		try {
			setPreferredSize(new Dimension(800,600));
			setPreferredSize(getMaximumSize());
			this.setLayout(null);
			{
				jLabelConfiguracoes = new JLabel();
				this.add(jLabelConfiguracoes);
				jLabelConfiguracoes.setText("CONFIGURACAO DA AQUISICAO DE DADOS DO SENSOR");
				jLabelConfiguracoes.setBounds(10, 11, 764, 14);
				jLabelConfiguracoes.setHorizontalAlignment(SwingConstants.CENTER);
			}
			{
				jButtonReconhecerPortas = new JButton();
				this.add(jButtonReconhecerPortas);
				jButtonReconhecerPortas.setText("Reconhecer Portas");
				jButtonReconhecerPortas.setBounds(10, 50, 149, 23);
				jButtonReconhecerPortas.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
				        jComboBoxEscolherPorta.removeAllItems();
				        SerialCom();
				        ListarPortas();
					}
				});
			}
			{
				jLabelEscolherPorta = new JLabel("Porta escolhida: ");
				this.add(jLabelEscolherPorta);
				jLabelEscolherPorta.setBounds(10, 95, 113, 14);
			}
			{
				ComboBoxModel jComboBoxEscolherPortaModel = 
						new DefaultComboBoxModel(
								new String[] {});
				jComboBoxEscolherPorta = new JComboBox();
				this.add(jComboBoxEscolherPorta);
				jComboBoxEscolherPorta.setModel(jComboBoxEscolherPortaModel);
				jComboBoxEscolherPorta.setBounds(133, 92, 175, 20);
			}
			{
				jLabelEscolherBaudRate = new JLabel("BaudRate escolhido: ");
				this.add(jLabelEscolherBaudRate);
				jLabelEscolherBaudRate.setBounds(10, 144, 149, 14);
			}
			{
				ComboBoxModel jComboBoxEscolherBaudRateModel = 
						new DefaultComboBoxModel(
								new Integer[] { 9600, 19200, 115200,128000});
				jComboBoxEscolherBaudRate = new JComboBox();
				this.add(jComboBoxEscolherBaudRate);
				jComboBoxEscolherBaudRate.setModel(jComboBoxEscolherBaudRateModel);
				jComboBoxEscolherBaudRate.setBounds(133, 141, 175, 20);
			}
			{
				//jButtonSalvar = new JButton();
				this.add(jbSalvar);
				jbSalvar.setBounds(233, 217, 75, 23);
				jbSalvar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						portaEscolhida = (String) jComboBoxEscolherPorta.getSelectedItem();
						baudrate = (Integer) jComboBoxEscolherBaudRate.getSelectedItem();	
					}
				});
			}
			{
				
				this.add(jbCancelar);
				jbCancelar.setBounds(149, 217, 74, 23);
			}
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}